using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	#endregion
	public class DonViChiSoesRepository
	{
        public static List<DonViChiSo> SelectAll()
		{
			return DataContext.Instance.DonViChiSoes.OrderBy(item => item.TenDVCS).ToList();
		}

        public static DonViChiSo SelectByID(int madvcs)
		{
			return DataContext.Instance.DonViChiSoes.FirstOrDefault(item => item.MaDVCS == madvcs );
		}

		public static bool Insert(DonViChiSo obj)
		{
			try
			{
				DataContext.Instance.DonViChiSoes.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int madvcs)
		{
			try
			{
				var delitem = DataContext.Instance.DonViChiSoes.FirstOrDefault(item => item.MaDVCS == madvcs );
				DataContext.Instance.DonViChiSoes.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

        public static List<DonViChiSo> RetrieveByID(int madvcs)
		{
			return (from item in DataContext.Instance.DonViChiSoes where  item.MaDVCS == madvcs  select item).ToList();
		}

	}
}